package com.example.device.maintain

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.device.R
import com.example.device.adapter.MaintainAdapter
import com.example.device.databinding.ActivityMaintainListBinding
import com.example.device.vm.MaintainViewModel
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/device/MaintainListActivity")
@AndroidEntryPoint
class MaintainListActivity : BaseMvvmActivity<ActivityMaintainListBinding>() {
    override fun getLayoutId(): Int  = R.layout.activity_maintain_list
    val maintainAdapter = MaintainAdapter()
    private val maintainViewModel : MaintainViewModel by viewModels()
    override fun initView() {
        super.initView()
        mDataBinding.back.setOnClickListener {
            finish()
        }
        mDataBinding.rv.layoutManager = LinearLayoutManager(this)
        mDataBinding.rv.adapter = maintainAdapter
        maintainViewModel.mSuccessLiveData.observe(this){
            maintainAdapter.data.clear()
            maintainAdapter.data.addAll(it.data)
            maintainAdapter.notifyDataSetChanged()
        }
        maintainViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
        mDataBinding.fab.setOnClickListener {
            ARouter.getInstance().build( "/device/MaintainAddActivity")
                .navigation()
        }
    }
    override fun onResume() {
        super.onResume()
       maintainViewModel.getMaintain()
    }
}